package com.qzdsoft.erpcloud.controller.sale;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qzdsoft.erpcloud.controller.BaseController;
import com.qzdsoft.erpcloud.service.com.CustomerSupplierStatementService;
import com.qzdsoft.erpcloud.service.proj.MaterialService;
import com.qzdsoft.erpcloud.service.sale.SaleQuotationDetailService;
import com.qzdsoft.erpcloud.service.sale.SaleQuotationService;
import com.qzdsoft.erpcloud.service.stock.StockStoreService;
import com.qzdsoft.erpcloud.service.sys.Permission;
import com.qzdsoft.erpcloud.service.sys.SysLog;
import com.qzdsoft.erpcloud.vo.com.CustomerSupplierStatementInfo;
import com.qzdsoft.erpcloud.vo.proj.MaterialInfo;
import com.qzdsoft.erpcloud.vo.proj.MaterialQueryInfo;
import com.qzdsoft.erpcloud.vo.sale.SaleQuotationDetailEditInfo;
import com.qzdsoft.erpcloud.vo.sale.SaleQuotationDetailQueryCriteriaViEn;
import com.qzdsoft.erpcloud.vo.sale.SaleQuotationDetailRemoveInfo;
import com.qzdsoft.erpcloud.vo.sale.SaleQuotationDetailViEn;
import com.qzdsoft.erpcloud.vo.sale.SaleQuotationViEn;
import com.qzdsoft.utils.ExceptionUtil;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.Response;
import com.qzdsoft.vo.ResultEnum;
import com.qzdsoft.vo.TypeInfo;
import com.qzdsoft.vo.proj.MaterialReviewStatus;

/***
 * 模块名：销售报价单明细controller
 * <p>详细描述：
 * @author DamingChen Sep 29, 2017
 * @see
 * @since 1.0
 */
@Controller
@RequestMapping("/sale/salequotation")
public class SaleQutotationDetailController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(SaleQutotationDetailController.class);
	private static final String BASEPATH = "sale/salequotation";

	@Autowired
	private SaleQuotationDetailService saleQuotationDetailService;

	@Autowired
	private SaleQuotationService saleQuotationService;

	@Autowired
	private MaterialService materialService;

	@Autowired
	private StockStoreService stockStoreService;

	@Autowired
	private CustomerSupplierStatementService customerSupplierStatementService;

//	@TablePermission(id = "16")
	@Permission(validate = false)
	@RequestMapping("/detail")
	public ModelAndView index(Integer id) {
		// 查询销售报价单
		SaleQuotationViEn saleQuotationInfo = saleQuotationService.findSaleQuotationBy(id);
		ModelAndView mv = new ModelAndView();
		mv.addObject("saleQuotation", saleQuotationInfo);
		mv.setViewName(BASEPATH + "/detail");
		return mv;
	}

	/**
	 * 跳转到页面
	 * 
	 * @return
	 */
	@RequestMapping("/order-details")
	public ModelAndView item(Integer id, SaleQuotationDetailQueryCriteriaViEn detailQueryCriteria) {
		// 查询物料信息
		MaterialQueryInfo info = new MaterialQueryInfo();
		info.setIsBuy(true);// 销售的物料
		info.setStatus(MaterialReviewStatus.HAS_REVIEW_CODE);// 查询已经审核的物料
		List<MaterialInfo> materialInfos = materialService.selectBy(info);
		ModelAndView mv = new ModelAndView();
		mv.addObject("materials", materialInfos);

		// 查询销售报价单
		SaleQuotationViEn saleQuotationInfo = saleQuotationService.findSaleQuotationBy(id);
		mv.addObject("saleQuotationItem", saleQuotationInfo);

		Integer companyId = saleQuotationInfo.getCompanyId();
		Integer customerId = saleQuotationInfo.getCustomerId();

		// 供应商结算方式
		List<CustomerSupplierStatementInfo> customerSupplierStatements = customerSupplierStatementService
				.selectBy(companyId, customerId);
		mv.addObject("statements", customerSupplierStatements);

		// 明细列表
		List<SaleQuotationDetailViEn> list = saleQuotationDetailService.selectDetailBy(detailQueryCriteria);
		mv.addObject("details", list);

		// 仓库列表
		List<TypeInfo> stores = stockStoreService.selectStockStore(Integer.parseInt(detailQueryCriteria.getCompanyId()));
		mv.addObject("stores", stores);

		mv.setViewName(BASEPATH + "/detailItem");
		return mv;
	}

	/**
	 * 跳转到页面
	 * 
	 * @return
	 */
	@SysLog(description = "编辑销售报价单明细")
	@ResponseBody
	@RequestMapping("/edit-orderdetail")
	public Response<String> edit(@RequestBody SaleQuotationDetailEditInfo info) {
		try {
			return saleQuotationDetailService.edit(info);
		} catch (Exception ex) {
			logger.error("编辑销售报价单明细错误，错误信息：{}", ExceptionUtil.getStackTrace(ex));
		}
		return new Response<String>(ResultEnum.ERROR);
	}
	
	@SysLog(description = "销售报价单详情删除")
	@ResponseBody
	@RequestMapping("/details/remove")
	public Response<String> remove(@RequestBody SaleQuotationDetailRemoveInfo info) {
		try {
			logger.debug("销售报价单详情删除，ids：{}", info.getIds());
			return saleQuotationDetailService.remove(info.getIds());
		} catch (Exception ex) {
			logger.error("编辑销售报价单明细错误，错误信息：{}", ExceptionUtil.getStackTrace(ex));
		}
		return new Response<String>(ResultEnum.ERROR);
	}
	/**
	 * 查询物料信息
	 * 
	 * @param info
	 * @return
	 */
	@Permission(validate = false)
	@ResponseBody
	@RequestMapping("/materias")
	public LayTableResponse<MaterialInfo> list(MaterialQueryInfo info) {
		info.setIsSale(true);
		info.setStatus(MaterialReviewStatus.HAS_REVIEW_CODE);// 查询已经审核的物料
		return materialService.selectPageBy(info);
	}

	/**
	 * 物料选择页面
	 * 
	 * @return
	 */
	@Permission(validate = false)
	@RequestMapping("/selectmaterials")
	public ModelAndView selectMaterial() {
		ModelAndView view = new ModelAndView();
		view.setViewName(BASEPATH + "/materials");
		return view;
	}
}
